package com.tgy.JUC.VolatileDemo;

import java.util.concurrent.atomic.AtomicInteger;

/***
 * @ClassName: Atomicity2
 * @Description: 原子类试用
 * @Auther: 送你一块二向箔
 * @Date: 16:38 2022/3/15
 * @version : V1.0
 */
public class Atomicity2 {
    private volatile static AtomicInteger num = new AtomicInteger();
    public static void main(String[] args) {
        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                for (int i1 = 0; i1 < 1000; i1++) {
                    add();
                }
            }).start();
        }


        while(Thread.activeCount()>2){
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName()+"  sum =====>"+num);
    }


    public static void add(){
        num.incrementAndGet();//AtomicInteger+1方法 CAS
    }
}
